﻿using Autofac.Core;
using DotNetCore.CAP;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Ongoal.Quotation.Cap;
using Ongoal.Quotation.Middleware;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ongoal.Quotation.CapExtensions
{
    /// <summary>
    /// 
    /// </summary>
    public static class MessageExtensions
    {

        /// <summary>
        /// Start the CAP transaction
        /// </summary>
        /// <param name="dbConnection">The <see cref="IDbConnection" />.</param>
        /// <param name="publisher">The <see cref="ICapPublisher" />.</param>
        /// <param name="autoCommit">Whether the transaction is automatically committed when the message is published</param>
        /// <returns>The <see cref="ICapTransaction" /> object.</returns>
        public static ICapTransaction BeginTransaction(this IDbConnection dbConnection,
            MessagePublisher publisher, bool autoCommit = false)
        {
            if (dbConnection.State == ConnectionState.Closed) dbConnection.Open();

            var dbTransaction = dbConnection.BeginTransaction();
            publisher.Transaction = ActivatorUtilities.CreateInstance<MySqlCapTransaction>(publisher.ServiceProvider);
            return publisher.Transaction;
        }

        /// <summary>
        /// Start the CAP transaction
        /// </summary>
        /// <param name="publisher">publisher</param>
        /// <param name="ado">ado</param>
        /// <param name="autoCommit">Whether the transaction is automatically committed when the message is published</param>
        /// <returns>The <see cref="ICapTransaction" /> object.</returns>
        public static ICapTransaction BeginTransaction(this MessagePublisher publisher,IAdo ado, bool autoCommit = false)
        {
            if (ado.Connection.State == ConnectionState.Closed) ado.Connection.Open();

            var dbTransaction = ado.Connection.BeginTransaction();
            
            publisher.Transaction = ActivatorUtilities.CreateInstance<MySqlCapTransaction>(publisher.ServiceProvider);
            //var tran =  publisher.Transaction.Begin(dbTransaction, autoCommit);
            //ado.Transaction= (IDbTransaction)tran.DbTransaction;//同步事务
            return publisher.Transaction;
        }

        /// <summary>
        /// Start the CAP transaction
        /// </summary>
        /// <param name="publisher">publisher</param>
        /// <param name="dbTransaction">db Transaction</param>
        /// <param name="autoCommit">Whether the transaction is automatically committed when the message is published</param>
        /// <returns>The <see cref="ICapTransaction" /> object.</returns>
        public static ICapTransaction BeginTransaction(this MessagePublisher publisher, IDbTransaction dbTransaction, bool autoCommit = false)
        {
            publisher.Transaction = ActivatorUtilities.CreateInstance<MySqlCapTransaction>(publisher.ServiceProvider);
            //var tran = publisher.Transaction.Begin(dbTransaction, autoCommit);
            return publisher.Transaction;
        }
    }
}
